package com.lechuang.user.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.lechuang.common.constant.BaseConstant;
import com.lechuang.common.model.TokenBaseInfo;
import com.lechuang.user.entity.User;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class LoginUtil {

    public static String createToken(User user) {
        Map<String,Object> payloadMap=new HashMap<>();
        Date signDate=new Date();
        //签发时间
        payloadMap.put(JWTPayload.ISSUED_AT, signDate);
        //生效时间
        payloadMap.put(JWTPayload.NOT_BEFORE, signDate);
        //过期时间
        payloadMap.put(JWTPayload.EXPIRES_AT, DateUtil.offsetDay(signDate,8));

        //将管理员用户信息放到token中
        TokenBaseInfo tokenBaseInfo = new TokenBaseInfo()
                .setAdminUserId(user.getId())
                .setAdminUserName(user.getPhone());
        payloadMap.put(BaseConstant.TOKEN_BEAN, tokenBaseInfo);
        return JWTUtil.createToken(payloadMap, BaseConstant.TOKEN_KEY.getBytes());
    }
}
